package com.sotb.sender;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 
 * @author lgb
 * 查看好友信息,设置发送内容
 */
public class FriendsActivity extends Activity{
	private Button addFriendBtn = null;
	//private Button backBtn = null;
	private Button commitBtn = null;
	//private ProgressBar progressBar = null;
	private TextView friendInfo = null;
	private EditText editText = null;
	private String name = "noName";
	private String number = "noNumber";
	private String content2Send = null;
	private boolean isThereFriend = false;
	SharedPreferences set = null;
	Editor editor = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.friend);
		addFriendBtn =  (Button) findViewById(R.id.add_friend);
		//backBtn = (Button) findViewById(R.id.back_to_setting);
		commitBtn = (Button) findViewById(R.id.commit);
		//progressBar = (ProgressBar) findViewById(R.id.progressBar);
		//progressBar.setVisibility(View.GONE);
		friendInfo = (TextView) findViewById(R.id.friend_info);
		editText = (EditText) findViewById(R.id.content_to_send);
		set = getSharedPreferences(Constant.PREFERENCE_NAME, MODE_PRIVATE);
		editor = set.edit();
		name = set.getString(Constant.NAME, name);
		number = set.getString(Constant.NUMBER, number);
		isThereFriend = set.getBoolean(Constant.IS_THERE_FRIEND, false);
		
		/*设置editText内容 */
		if (content2Send != null){
			editText.setHint(content2Send);
		}
		
		Intent intent = getIntent();
		Bundle bundle = intent.getExtras();
		/*读取好友信息*/	
		if (bundle != null){			
			name = bundle.getString(Constant.NAME);
			number = bundle.getString(Constant.NUMBER);			
			/*读取后已有好友*/
			isThereFriend = true;		
		}
		
        /*已有好友则将标题更改为更换好友*/
		if (isThereFriend){
			addFriendBtn.setText(R.string.change_friend);
			friendInfo.setText(name+number);
		}
		
		content2Send = set.getString(Constant.CONTENT_TO_SEND, null);
		
		/*保存好友设置*/
		editor.putString(Constant.NAME, name);
		editor.putString(Constant.NUMBER, number);
		editor.putBoolean(Constant.IS_THERE_FRIEND, isThereFriend);
		//这里不commit的话会导致TextView显示滞后
		editor.commit();
		
		addFriendBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				//progressBar.setVisibility(View.VISIBLE);
				Intent intent = new Intent();
				intent.setClass(FriendsActivity.this, ContactListActivity.class);
				startActivity(intent);
			}
		});
		
//		backBtn.setOnClickListener(new OnClickListener() {
//			
//			@Override
//			public void onClick(View v) {
//				//保存一下
//				if (editor != null){
//					editor.commit();
//				}
//				Intent intent = new Intent();
//				intent.setClass(FriendsActivity.this, SettingActivity.class);
//				startActivity(intent);
//				finish();
//			}
//		});
		
		commitBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				//保存content2Send
				content2Send = editText.getText().toString();
				editor.putString(Constant.CONTENT_TO_SEND, content2Send);
				editor.commit();
				Toast.makeText(FriendsActivity.this, "修改内容成功", Toast.LENGTH_SHORT).show();
			}
		});
		
	}
	
	/*保存设置*/
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		if (editor != null){
			editor.commit();
		}		
		super.onPause();
	}
	
	
	/*读取设置*/
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		//读取设置
        name = set.getString(Constant.NAME, name);
		number = set.getString(Constant.NUMBER, number);
		isThereFriend = set.getBoolean(Constant.IS_EXPERIENCE, false);
		//设置好已保存的发送内容
		content2Send = set.getString(Constant.CONTENT_TO_SEND, null);
		editText.setHint(content2Send);
		//progressBar.setVisibility(View.GONE);
		if (name.equals("noName")){
			friendInfo.setText("没有好友");
		}
		else{
			friendInfo.setText(name+number);
		}
		super.onResume();
	}

}
